<?php

namespace App\Service;


use Illuminate\Support\Facades\Redis;


class RedisService {

    /**
     * @param $token
     * @param $arr 用户信息
     * @return array
     */
    public function add($token,$arr){
        $arr=json_encode($arr);
        $value = Redis::set($token,$arr);
        if(!$value){
            return ['msg'=>'操作失败','code'=>2001];
        }
        $val = Redis::expire($token,3600);
        if($val != 1){
            return ['msg'=>'操作失败','code'=>2003];
        }
        return ['msg'=>'操作成功','code'=>200];
    }

    /**
     * @param $token
     * @return array
     */
    public function renewal($token){
        $value = Redis::exists($token);
        if($value != 1){
           return ['msg'=>'token不存在','code'=>2001];
        }
        $val = Redis::expire($token,3600);
        if($val != 1){
            return ['msg'=>'续约失败','code'=>2003];
        }
        $user = Redis::get($token);

        return ['msg'=>'操作成功','code'=>200,'data'=>$user];
    }

    /**
     * @param $token
     * @return array
     */
    public function del($token){
        $value = Redis::del($token);
        if($value < 1){
            return ['msg'=>'操作失败','code'=>2002];
        }
        return ['msg'=>'操作成功','code'=>200];
    }

}